<?php
/**
 * This file is part of the Jindai.
 * @copyright Copyright (c) 2019 All Rights Reserved.
 * @author jindai <jindai0305@gmail.com>
 */

namespace App\Models;

use App\Models\traits\PublishTrait;
use App\Models\traits\SoftDeleteTrait;
use App\Observers\CommentObserver;

/**
 * Class Comment
 * @package App\Models
 *
 * @property integer $id
 * @property integer $user_id
 * @property integer $item_id
 * @property integer $reply_id
 * @property integer $parent_id
 * @property string $content
 * @property integer $favor
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $deleted_at
 * @property integer $flag
 *
 * @property-read User $user
 * @property-read Item $item
 * @property-read UserRelation $userRelation
 * @property-read User $replyUser
 * @property-read Comment $childrenLimitThree
 *
 * @method \Illuminate\Database\Eloquent\Builder|static basic
 * @method \Illuminate\Database\Eloquent\Builder|static withChildren(int $limit)
 */
class Comment extends BaseModel
{
    use PublishTrait, SoftDeleteTrait;

    protected $fillable = [
        'content', 'status', 'reply_id', 'user_id', 'parent_id', 'item_id', 'flag'
    ];

    /**
     * @inheritdoc
     */
    public function getTable()
    {
        return 'comments';
    }

    /**
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeBasic($query)
    {
        return $query->where('parent_id', '=', 0);
    }

    /**
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param int $limit
     * @return \Illuminate\Database\Eloquent\Builder mixed
     */
    public function scopeWithChildren($query, $limit = 3)
    {
        return $query->with(['childrenLimitThree' => function ($query) use ($limit) {
            /** @var \Illuminate\Database\Eloquent\Builder $query */
            return $query->limit($limit);
        }]);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function item()
    {
        return $this->belongsTo(Item::class, 'item_id', 'id');
    }

    /**
     * @inheritdoc
     */
    static protected function getObserver()
    {
        return CommentObserver::class;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function userRelation()
    {
        return $this->hasOne(UserRelation::class, 'relation_id', 'id')
            ->where('type', '=', UserRelation::TYPE_COMMENT)
            ->where('user_id', '=', \Auth::guard('api')->id());
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function childrenLimitThree()
    {
        return $this->hasMany(static::class, 'parent_id', 'id')->orderBy('created_at', 'desc')->with(['item', 'user.userExpand', 'replyUser.userExpand']);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function replyUser()
    {
        return $this->belongsTo(User::class, 'reply_id', 'id');
    }

    /**
     * @return bool
     */
    public function hasReply()
    {
        return $this->reply_id !== -1;
    }
}
